﻿using CommonUtils;
using System;
using System.Collections;
using System.IO;
using System.Runtime.InteropServices;
using System.Xml;

namespace DatabaseService.DatabaseDAO
{
    /// <summary>
    /// 记录sql日志
    /// </summary>
    internal class Sql
    {
        string sqlStr;
        bool islog;

        /// <summary>
        /// sql文本
        /// </summary>
        public string SqlStr { get => sqlStr; set => sqlStr = value; }
        /// <summary>
        /// 是否输出到日志
        /// </summary>
        public bool Islog { get => islog; set => islog = value; }
    }

    /// <summary>
    /// 泛型sql缓存
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class StaticTableModelSqlCache<T> where T : TableModelMapper, new()
    {
        private static Hashtable hashtable;
        private static XmlDocument document;
        //private static string filepath;
        static StaticTableModelSqlCache()
        {
            hashtable = new Hashtable();
            T entity = new T();
            string fullName = entity.GetMappperPath();  //根据继承虚方法获取mapper目录,默认放在运行目录下的Mapper文件夹下
            try
            {
                document = new XmlDocument();
                if (PathManager.IsUseMapperXMLFile)
                {
                    //固定目录--Mapper
                    var filepath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fullName);
                    document = new XmlDocument();
                    document.Load(filepath);
                }
                else
                {
                    var _assembly = entity.GetType().Assembly;
                    Stream stream = _assembly.GetManifestResourceStream(fullName);
                    document.Load(stream);
                }

                var mapper = document.SelectSingleNode("mapper");
                if (mapper != null)
                {
                    foreach (var node in mapper)
                    {
                        if (!(node is XmlElement))
                            continue;
                        XmlElement ele = node as XmlElement;
                        string method = ele.GetAttribute("id");
                        if (string.IsNullOrWhiteSpace(method))
                            continue;
                        string sql = ele.InnerText.Trim();
                        if (!hashtable.ContainsKey(method))
                            hashtable.Add(method, new Sql { Islog = ele.GetAttribute("log") == "T", SqlStr = sql });
                    }
                }
            }
            catch (Exception e)
            {

            }
        }

        public static string Getsql(string method, string param = "")
        {
            string sql = "";
            if (hashtable.ContainsKey(method))
            {
                Sql sqlpkg = hashtable[method] as Sql;
                if (sqlpkg.Islog)
                {
                    //记录日志
                }
                sql = sqlpkg.SqlStr;
            }

            return sql;
        }
    }
}
